package com.dhcc.bpm.modules.log.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.log.entity.OperLog;
import com.dhcc.bpm.modules.log.service.IOperLogService;
import com.dhcc.bpm.modules.log.vo.OperLogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * @ClassName OperLogController.class
 * @Description  操作日志记录 控制器
 * @Author xiaFaYu
 * @Date 2020-02-03 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-log/operlog")
@Api(value = "操作日志记录", tags = "操作日志记录接口")
public class OperLogController extends BladeController {
	private  final static Logger log = LoggerFactory.getLogger(OperLogController.class);
	private IOperLogService operLogService;

	/**
	 * @Description 详情
	 * @Author xiafayu
	 * @Date 2020/2/24 15:14
	 * @param operLog
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.log.entity.OperLog>
	 **/
	@ApiLog("操作日志记录详情")
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入operLog")
	@Log(title = "查询操作日志记录详情", businessType = BusinessType.SELECT)
	public R<OperLog> detail(OperLog operLog) {
		OperLog detail = operLogService.getOne(Condition.getQueryWrapper(operLog));
		return R.data(detail);
	}

	/**
	 * @Description 分页 操作日志记录
	 * @Author xiafayu
	 * @Date 2020/2/24 15:14
	 * @param operLog
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.log.entity.OperLog>>
	 **/
	@ApiLog("操作日志记录分页")
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入operLog")
	@Log(title = "查询操作日志记录", businessType = BusinessType.SELECT)
	public R<IPage<OperLog>> list(@ApiIgnore @RequestParam Map<String, Object> operLog, Query query) {
		IPage<OperLog> pages = operLogService.page(Condition.getPage(query), Condition.getQueryWrapper(operLog, OperLog.class));
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 操作日志记录
	 * @Author xiafayu
	 * @Date 2020/2/24 15:14
	 * @param operLog
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.log.vo.OperLogVO>>
	 **/
	@ApiLog("操作日志记录分页")
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入operLog")
	@Log(title = "查询操作日志记录", businessType = BusinessType.SELECT)
	public R<IPage<OperLogVO>> page(OperLogVO operLog, Query query) {
		if (operLog.getOperDate() != null && operLog.getOperDate().length() > 0) {
			String startDate = operLog.getOperDate().split(",")[0];
			String endDate = operLog.getOperDate().split(",")[1];
			operLog.setStartDate(startDate);
			operLog.setEndDate(endDate);
		}
		IPage<OperLogVO> pages = operLogService.selectOperLogPage(Condition.getPage(query), operLog);
		return R.data(pages);
	}

	/**
	 * @Description 新增 操作日志记录
	 * @Author xiafayu
	 * @Date 2020/2/24 15:14
	 * @param operLog
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/save")
	@ApiLog("操作日志记录新增")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入operLog")
	@Log(title = "新增操作日志记录", businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody OperLog operLog) {
		return R.status(operLogService.save(operLog));
	}

	/**
	 * @Description 修改 操作日志记录
	 * @Author xiafayu
	 * @Date 2020/2/24 15:14
	 * @param operLog
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiLog("操作日志记录修改")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入operLog")
	@Log(title = "修改操作日志记录", businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody OperLog operLog) {
		return R.status(operLogService.updateById(operLog));
	}

	/**
	 * @Description 新增或修改 操作日志记录
	 * @Author xiafayu
	 * @Date 2020/2/24 15:15
	 * @param operLog
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiLog("操作日志记录新增或修改")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入operLog")
	@Log(title = "修改操作日志记录", businessType = BusinessType.UPDATE)
	public R submit(@Valid @RequestBody OperLog operLog) {
		return R.status(operLogService.saveOrUpdate(operLog));
	}

	
	/**
	 * @Description 删除 操作日志记录
	 * @Author xiafayu
	 * @Date 2020/2/24 15:15
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiLog("操作日志记录删除")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@Log(title = "删除操作日志记录", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(operLogService.deleteLogic(Func.toLongList(ids)));
	}

	
}
